<template>
  <div class="idle-manage">
    <!-- 搜索栏 -->
    <el-card class="search-card" shadow="never">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="商品名称">
          <el-input v-model="searchForm.findValue" placeholder="请输入商品名称" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" style="width: 150px">
            <el-option label="已发布" :value="1"></el-option>
            <el-option label="已下架" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card" shadow="never">
      <el-table :data="tableData" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="idleName" label="商品名称" min-width="150"></el-table-column>
        <el-table-column prop="idlePrice" label="价格" width="100">
          <template #default="{ row }">
            <span style="color: #ec4899; font-weight: 600">¥{{ row.idlePrice }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="idleLabel" label="分类" width="120">
          <template #default="{ row }">
            <el-tag :type="getTagType(row.idleLabel)">{{ getCategoryName(row.idleLabel) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="idlePlace" label="地区" width="120"></el-table-column>
        <el-table-column prop="idleStatus" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.idleStatus === 1 ? 'success' : 'info'">
              {{ row.idleStatus === 1 ? '已发布' : '已下架' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="releaseTime" label="发布时间" width="180"></el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="row.idleStatus === 1" 
              type="warning" 
              size="small" 
              @click="handleUpdateStatus(row, 2)">
              下架
            </el-button>
            <el-button 
              v-else 
              type="success" 
              size="small" 
              @click="handleUpdateStatus(row, 1)">
              上架
            </el-button>
            <el-button 
              type="danger" 
              size="small" 
              @click="handleUpdateStatus(row, 0)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        :current-page="pagination.page"
        :page-size="pagination.nums"
        :total="pagination.total"
        :page-sizes="[8, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; justify-content: flex-end">
      </el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api'

const loading = ref(false)
const tableData = ref([])

const searchForm = reactive({
  findValue: '',
  status: 1
})

const pagination = reactive({
  page: 1,
  nums: 8,
  total: 0
})

const categories = {
  1: '数码科技',
  2: '生活用品',
  3: '运动相关',
  4: '图书笔记',
  5: '公告'
}

const getCategoryName = (label) => {
  return categories[label] || '其他'
}

const getTagType = (label) => {
  const types = { 1: 'primary', 2: 'success', 3: 'warning', 4: 'info', 5: 'danger' }
  return types[label] || 'info'
}

const loadData = () => {
  loading.value = true
  const params = {
    findValue: searchForm.findValue || '',
    status: searchForm.status,
    page: pagination.page,
    nums: pagination.nums
  }
  
  api.queryIdle(params).then(res => {
    loading.value = false
    if (res.status_code === 1) {
      tableData.value = res.data.records || []
      pagination.total = res.data.total || 0
    } else {
      ElMessage.error(res.msg || '获取数据失败')
    }
  }).catch(() => {
    loading.value = false
  })
}

const handleSearch = () => {
  pagination.page = 1
  loadData()
}

const handleReset = () => {
  searchForm.findValue = ''
  searchForm.status = 1
  pagination.page = 1
  loadData()
}

const handleSizeChange = (val) => {
  pagination.nums = val
  pagination.page = 1
  loadData()
}

const handleCurrentChange = (val) => {
  pagination.page = val
  loadData()
}

const handleUpdateStatus = (row, status) => {
  const actionText = status === 0 ? '删除' : (status === 1 ? '上架' : '下架')
  ElMessageBox.confirm(`确定要${actionText}该商品吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    api.updateIdleStatus({ id: row.id, status }).then(res => {
      if (res.status_code === 1) {
        ElMessage.success(`${actionText}成功`)
        loadData()
      } else {
        ElMessage.error(res.msg || `${actionText}失败`)
      }
    })
  }).catch(() => {})
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.idle-manage {
  height: 100%;
}

.search-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.table-card {
  border-radius: 8px;
}
</style>
